<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: PromotionController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Kowcore_PromotionController extends Core_Controller_Action_Standard {
	public function promoteAction() {
		$this->view->object = $object = Engine_Api::_ ()->kowcore ()->ofr ();
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if (! $viewer->getIdentity ()) {
			$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Login first.' );
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Login first.' ) ) ) );
			return;
		}
		
		if (! $this->getRequest ()->isPost ()) {
			$this->view->paginator = $friends = $paginator = Engine_Api::_ ()->kowcore ()->getFriends ();
			if (! $paginator->getTotalItemCount ()) {
				$this->view->message = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You have no friends to whom you can promote.' );
				$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'You have no friends to whom you can promote.' ) ) ) );
				return;
			}
			
			$this->view->page = $page = $this->_getParam ( 'page', 1 );
			$paginator->setItemCountPerPage ( $this->_getParam ( 'ipp', 15 ) );
			$paginator->setCurrentPageNumber ( $page );
			
			return;
		}
		
		$values = $this->getRequest ()->getPost ();
		$db = $this->_helper->api ()->getDbtable ( 'messages', 'messages' )->getAdapter ();
		$db->beginTransaction ();
		
		try {
			$viewer = $this->_helper->api ()->user ()->getViewer ();
			$recipients = $values ['uids'];
			$recipients = array_unique ( $recipients );
			
			$recipientsUsers = Engine_Api::_ ()->getItemMulti ( 'user', $recipients );
			
			$conversation = Engine_Api::_ ()->getItemTable ( 'messages_conversation' )->send ( $viewer, $recipients, $values ['subject'], $values ['message'] );
			
			foreach ( $recipientsUsers as $user ) {
				if ($user->getIdentity () == $viewer->getIdentity ()) {
					continue;
				}
				Engine_Api::_ ()->getDbtable ( 'notifications', 'activity' )->addNotification ( $user, $viewer, $conversation, 'message_new' );
			}
			
			// Increment messages counter
			Engine_Api::_ ()->getDbtable ( 'statistics', 'core' )->increment ( 'messages.creations' );
			
			$db->commit ();
			
			return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => false, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Your message was successfully sent.' ) ) ) );
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	}
}